<template>
  <view class="card-item">
    <view class="item-content border-bottom">
      <view class="left-box">
        <image :src="mypending.urgency === 0?'https://mkservice.mk-iot.com/subFile/mkApplet/report.png':'https://mkservice.mk-iot.com/subFile/mkApplet/setmaintenance.png'"
          mode="aspectFill"></image>
      </view>
      <view class="right-box">
        <view class="floor">{{mypending.maintenanceCode}}
          <view class="level-tag"
            :style="mypending.urgency === 0?'background-color:#07c160' : 'background-color:#ee0a24'">
            {{mypending.urgency === 0?'普通维修':'紧急维修'}}
          </view>
        </view>
        <text class="date">{{mypending.warrantyDate}}</text>
        <text class="state pending" v-if="mypending.maintenanceStatus == 0">待派发</text>
        <text class="state processing" v-if="mypending.maintenanceStatus==1">维修中</text>
        <text class="state audit" v-if="mypending.maintenanceStatus==2">已完成-(待审核)</text>
        <text class="state pending" v-if="mypending.maintenanceStatus ==3">已完成-(已拒绝)</text>
        <text class="state audited" v-if="mypending.maintenanceStatus==4">已完成-(已同意)</text>
        <text class="state audit" <text class="desc">{{mypending.taskDescription}}</text>
        <view class="item-footer">
          <text class="delrepair" v-if="mypending.maintenanceStatus == 0 && roleIsAdmin ==true"
            @tap="deleteMeSend(mypending.maintenanceCode)">删除报修</text>
          <text class="distribution" v-if="mypending.maintenanceStatus == 0 && roleIsAdmin==true"
            @tap="distribution(mypending.maintenanceCode,mypending.warrantyPersonId,1,mypending.urgency)">派发维修员</text>
            <text class="distribution" v-if="mypending.maintenanceStatus == 1 && roleIsAdmin==true"
              @tap="distribution(mypending.maintenanceCode,mypending.warrantyPersonId,2,mypending.urgency)">重新派发</text>
          <text class="distribution" v-if="mypending.maintenanceStatus == 0 
              && mypending.maintainerId!=null && roleIsAdmin== false"
            @tap="isDetermine(mypending.maintenanceCode,mypending.warrantyPersonId,mypending.urgency)">是否确定维修</text>
          <text class="completed" v-if="mypending.maintenanceStatus == 1 || mypending.maintenanceStatus == 3"
            @tap="updateMaintenanceStatus(mypending.maintenanceCode,mypending.warrantyPersonId)">维修完成</text>
          <text class="evaluateitem" v-if="mypending.maintainerEvaluate !== null"
            @tap="userEvaluate(mypending.maintenanceCode)">查看评价</text>
          <text class="queryitem"
            @tap="navDetail(mypending.maintenanceCode,mypending.maintainerId,mypending.maintainerId)">查看详情</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import {
    delTask,
    updateTask,
    selectAllMaintenanId,
    addTaskState,
    sendScheduleMessage,
    sendMessage
  } from "@/api/system/task/task.js"
  import getDateTime from '../../../utils/getdateTime.js'
  import {
    getUserItme
  } from "@/api/system/user"
  export default {
    name: "my-pending",
    // 定义 props 属性，用来接收外界传递到当前组件的数据
    props: {
      //  父级传递过来对象
      mypending: {
        type: Object,
        default: {}
      },
    }, 
    data() {
      return {
      roleIsAdmin: this.$auth.hasRole("admin")
      }
    },
     methods: {
       //删除报修
       deleteMeSend(maintenanceCode) {
         this.$modal.confirm('确定删除此次报修申请吗？').then(() => {
           delTask(maintenanceCode).then(response => {
             if (response.code === 200) {
               //重置关键数据
              this.$parent.empty();
              this.$parent.queryStateList();
              this.$parent.queryNumberByState();
              this.$modal.msgSuccess("删除成功！")
             }
           })
         })
       },
       //是否确定维修该工单
       isDetermine(maintenanceCode, warrantyPersonId, urgency) {
         const thant = this
         uni.showModal({
           title: '提示',
           content: '是否确定去维修本次报修单？',
           confirmText: "是",
           cancelText: "否",
           success: function(res) {
             if (res.confirm) {
               const data = {
                 maintenanceCode: maintenanceCode,
                 maintenanceStatus: 1,
                 startTime: getDateTime.dateTimeStr('y-m-d h:i:s')
               }
               //派发成功后在记录表新增记录
               thant.$parent.maintenanceCode = maintenanceCode
               thant.$parent.stateData.state = "维修中"
               addTaskState(thant.$parent.stateData).then(response => {
                 if (response.code == 200) {
                   updateTask(data).then(response => {
                     if (response.code === 200) {
                       getUserItme(warrantyPersonId).then(response => {
                         if (response.code == 200 && response.data.openId != null) {
                           //新增成功调用接口
                           const data = {
                             openid: response.data.openId,
                             selfPage: '/pages/login',
                             maintenanceCode: maintenanceCode,
                             maintenanceStatus: "已接单",
                             description: urgency == 0 ? "已接单,预计最晚" + thant.$moment().add('7', 'd')
                               .format('YYYY-MM-DD') + "处理" : "已接单,预计最晚" + thant.$moment().add('3',
                                 'd').format('YYYY-MM-DD') + "处理",
                           }
                           sendScheduleMessage(data).then(response => {
                             //操作成功跳转到当前处理中
                             thant.$parent.chooseTab = 1
                             thant.$parent.queryParams.maintenanceStatus = 1
                             //重置关键数据
                             thant.$parent.empty()
                             thant.$parent.queryStateList()
                             thant.$parent.queryNumberByState()
                             thant.$parent.$refs.popup.close()
                             if (response.errcode == 0) {
                               thant.$modal.msgSuccess("操作成功")
                             }
                           })
                         } else {
                           //重置关键数据
                            thant.$parent.chooseTab = 1
                            thant.$parent.queryParams.maintenanceStatus = 1
                            thant.$parent.empty()
                            thant.$parent.queryStateList()
                            thant.$parent.queryNumberByState()
                            thant.$parent.$refs.popup.close()
                            thant.$modal.msgSuccess("操作成功")
                         }
                       })
                     }
                   });
                 }
               })
             } else if (res.cancel) {
               //点击否的话重新自动派单
               selectAllMaintenanId({
                 userId: thant.userId
               }).then(response => {
                 if (response.code == 200 && response.rows[0] != null) {
                   const maintainerId = response.rows[0].user.userId
                   //根据工单id重新派发工单人员
                   const data = {
                     maintainerId: maintainerId,
                     maintenanceCode: maintenanceCode
                   }
                    thant.$parent.stateData.maintenanceCode = maintenanceCode
                    thant.$parent.stateData.state = "维修员拒绝维修"
                    thant.$parent.stateData.descs = "该报修单已经被维修员拒绝，已经重新派发其他维修员进行维修，请见谅"
                   addTaskState(thant.$parent.stateData).then(response => {
                     if (response.code == 200) {
                       updateTask(data).then(response => {
                         if (response.code === 200) {
                           getUserItme(warrantyPersonId).then(response => {
                             if (response.code == 200 && response.data.openId != null) {
                               //新增成功调用接口
                               thant.$parent.nickName = response.data.nickName
                               thant.$parent.phonenumber = response.data.phonenumber
                               const scheduleMessagedata = {
                                 openid: response.data.openId,
                                 selfPage: '/pages/login',
                                 maintenanceCode: maintenanceCode,
                                 maintenanceStatus: "已拒绝接单",
                                 description: "维修人员已拒绝接单,已经派发其他维修员",
                               }
                               sendScheduleMessage(scheduleMessagedata).then(res => {
                                 if (res.errcode == 0) {
                                   getUserItme(maintainerId).then(response => {
                                     if (response.code == 200 && response.data.openId !=
                                       null) {
                                       //新增成功调用接口
                                       const messagedata = {
                                         openid: response.data.openId,
                                         selfPage: '/pages/login',
                                         maintenanceCode: maintenanceCode,
                                         urgency: urgency == 0 ? '普通维修' : '紧急维修',
                                         username: thant.$parent.nickName,
                                         phonenumber: thant.$parent.phonenumber,
                                         description: urgency == 0 ?
                                           '您有一份普通维修单,请在7天内及时处理' :
                                           '您有一份紧急维修单,请在3天内及时处理'
                                       }
                                       sendMessage(messagedata).then(response => {
                                         thant.$parent.empty()
                                         thant.$parent.queryStateList()
                                         thant.$parent.queryNumberByState()
                                         if (response.errcode == 0) {
                                           thant.$modal.msgSuccess("重新派发成功")
                                         } else {
                                           thant.$modal.msgError("订阅失败");
                                         }
                                       })
                                     }
                                   })
                                 } else {
                                   thant.$parent.empty()
                                   thant.$parent.queryStateList()
                                   thant.$parent.queryNumberByState()
                                   thant.$modal.msgError("订阅失败");
                                 }
                               })
                             } else {
                               thant.$parent.empty()
                               thant.$parent.queryStateList()
                               thant.$parent.queryNumberByState()
                               thant.$modal.msgSuccess("重新派发成功")
                             }
                           })
                         }
                       })
                     }
                   })
                 } else {
                   uni.showModal({
                     title: '提示',
                     content: '暂无其他维修员可派发，请联系管理员',
                     showCancel: false,
                   });
                 }
               })
             }
           }
         })
       },
       //管理员点击派发人员
        distribution(maintenanceCode, warrantyPersonId,distributionType,urgency) {
          this.$parent.maintenanceCode = maintenanceCode
          this.$parent.distributionType=distributionType
          this.$parent.warrantyPersonId=warrantyPersonId
          this.$parent.urgency=urgency
          this.$parent.getuserList()
          this.$parent.$refs.popup.open()
        },
        //管理员人员进行审核（修改维修状态）
        updateMaintenanceStatus(maintenanceCode, warrantyPersonId) {
            this.$modal.confirm('确定完成了本次维修了吗？').then(() => {
            //清空维修完成的图片和描述
            this.$parent.repairImgList=[]
            this.$parent.formData.repairDescription=""
             //带参数给父类
            this.$parent.maintenanceCode=maintenanceCode
            this.$parent.warrantyPersonId=warrantyPersonId
            this.$parent.$refs.popupshow.open()
          })
        },
        //查看详细页
        navDetail(mcode, maintainerId, userid) {
          this.$tab.navigateTo('/subpkg/maintenance/maintenance_item?mcode=' + mcode + "&maintainerId=" +
            maintainerId + "&userid=" + userid)
        },
        //查看评价
        userEvaluate(maintenanceCode) {
          this.$tab.navigateTo('/subpkg/maintenance/maintenance_evaluate?maintenanceCode=' +
            maintenanceCode)
        },
     }
  }
</script>

<style lang="scss">
  .card-item {
    margin: 10rpx;
    border-radius: 10rpx;
    position: relative;

    .item-content {
      background-color: white;
      display: flex;
      padding: 10rpx;
      position: relative;

      .left-box>image {
        width: 100rpx;
        height: 100rpx;
        border-radius: 10rpx;
        border: 2px solid rgb(209, 209, 209);
      }

      .right-box {
        display: flex;
        flex: 1;
        flex-direction: column;
        margin-left: 20rpx;

        .floor {
          font-weight: bold;
          font-size: 32rpx;

          .level-tag {
            font-size: 23rpx;
            color: white;
            margin-left: 5px;
            padding: 5rpx 12rpx;
            display: inline-block;
            background-color: danger;
            width: 60px;
            text-align: center;
          }
        }

        .state {
          text-align: center;
          width: 50px;
          color: #fff;
          font-size: 28rpx;
          margin: 5rpx 0;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }

        .pending {
          background-color: #ee0a24;
          width: 100px;
        }

        .processing {
          background-color: #ff976a;
          width: 100px;
        }

        .audit {
          background-color: #00aaff;
          width: 100px;
        }

        .audited {
          background-color: #07c160;
          width: 100px;
        }

        .desc {
          font-size: 28rpx;
          margin: 5rpx 0;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }

        .date {
          font-size: 28rpx;
          color: gray;
          margin-top: 5rpx;
        }
      }

      .item-footer {
        font-size: 28rpx;
        padding: 10rpx;
        text-align: right;
      }

      .item-footer>.delrepair {
        border: 1px solid red;
        padding: 5rpx 10rpx 5rpx 10rpx;
        border-radius: 15rpx;
        color: red;
      }

      .item-footer>.distribution {
        margin-left: 20rpx;
        border: 1px solid orange;
        padding: 5rpx 10rpx 5rpx 10rpx;
        border-radius: 15rpx;
        color: orange;
      }

      .item-footer>.completed {
        margin-left: 20rpx;
        color: rgb(31, 134, 0);
        border: 1px solid rgb(31, 134, 0);
        padding: 5rpx 10rpx 5rpx 10rpx;
        border-radius: 15rpx;
      }

      .item-footer>.evaluateitem {
        margin-left: 20rpx;
        padding: 5rpx 10rpx 5rpx 10rpx;
        border-radius: 15rpx;
        color: rgb(148, 148, 148);
        border: 1px solid rgb(148, 148, 148);
      }

      .item-footer>.queryitem {
        margin-left: 20rpx;
        border: 1px solid #1989FA;
        padding: 5rpx 10rpx 5rpx 10rpx;
        border-radius: 15rpx;
      }
    }
  }
</style>